package com.atguigu.exer;
import java.util.concurrent.Callable;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.FutureTask;

//创建线程的方式3:实现Callable接口
public class CallableTest {
    public static void main(String[] args) {
        NumThread numThread=new NumThread();
        FutureTask futureTask=new FutureTask(numThread);
        Thread thread=new Thread(futureTask);
        thread.start();
        try {
            //get的方法的返回值即为FutureTask构造器参数Callable实现类重写的call方法的返回值
            Object sum=futureTask.get();
            System.out.println("总和为:"+sum);
        }catch(InterruptedException e){
            e.printStackTrace();
        }catch(ExecutionException e){
            e.printStackTrace();
        }
    }
}
class NumThread implements Callable{
    public Object call() throws Exception{
        int sum=0;
        for(int i=1;i<=100;i++){
            if(i%2==0){
                System.out.println(i);
                sum+=i;
            }
        }
        return sum;
        }
}
